const router=require('koa-router')();
const {User,Post,Comment}=require('../models/index');

//积分top100
router.get('/top100',async ctx=>{
    let users=await User.find(null,{name:true,avatar:true,score:true},{skip:0,limit:100,sort:{score:-1}});
    let [postCounts,commentCounts,top10,noReplies]=await Promise.all([
        Promise.all(users.map(user=>Post.count({author:user._id}))),
        Promise.all(users.map(user=>Comment.count({author:user._id}))),
        User.find(null,{name:true,score:true},{skip:0,limit:5,sort:{score:-1}}),
        Post.find({comments:{$size:0}},{title:true},{skip:0,limit:5,sort:{score:-1}})
    ]);

    await ctx.render('top100',{
        title:'Top100积分榜',
        users,
        postCounts,
        commentCounts,
        top10,
        noReplies,
        loginUser:ctx.session.loginUser,
        success:ctx.flash.success,
        error:ctx.flash.error
    });
});

module.exports=router;